﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StudentMgmt.Model;
using StudentMgmt.SQLServerDAL;

namespace StudentMgmt
{
    public partial class frmNewChange : Form
    {
        public frmNewChange()
        {
            InitializeComponent();
        }

        private void frmNewChange_Load(object sender, EventArgs e)
        {
            //初始化控件
            ChangeConfig changeConfig = new ChangeConfig();
            IList<Change_ConfigInfo> changeConfigInfo = changeConfig.GetChangeConfig();
            this.cmbStatusChange.DataSource = changeConfigInfo;
            this.cmbStatusChange.DisplayMember = "Change_Descriptions";
            this.cmbStatusChange.ValueMember = "Change_Code";

        }
        private void cmbDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            //部门下拉列表变化时班级下拉列表随之改变
            Classes myclass = new Classes();
            IList<ClassInfo> classInfo = myclass.GetClassesByDeptId(this.cmbDepartment.SelectedValue.ToString());
            this.cmbClass.DataSource = classInfo;
            this.cmbClass.DisplayMember = "ClassName";
            this.cmbClass.ValueMember = "ClassId";
        }

        void SetdgvChanLog()
        {
            Changes change = new Changes();
            DataTable changeTable = new DataTable();
            int tag = change.QueryChanges(this.tbStuNo.Text.Trim(), out changeTable);
            if (tag == 1 && changeTable.Rows.Count > 0)
            {
                dgvChanLog.DataSource = changeTable;
                dgvChanLog.Columns[0].Visible = false;
            }
            else
            {
                dgvChanLog.DataSource = null;
            }               
        }
        private void btuSearch_Click(object sender, EventArgs e)
        {
            //搜索
            string stuNo = this.tbStuNo.Text.Trim();
            Student student = new Student();
            DataTable myTable = new DataTable();
            int val = 0;
            if (stuNo != "")
            {
                val = student.QueryStudent(stuNo, "", "", "", out myTable);
            }
            else
            {
                MessageBox.Show("未输入学号进行查询！");
                return;
            }
            //显示学生信息
            if (val == 1 && myTable.Rows.Count > 0)
            {
                //查询当前状态
                this.tbStuName.Text = myTable.Rows[0]["学生姓名"].ToString();
                Changes change = new Changes();
                ChangeInfo changeInfo = change.GetLastChangeByStuNo(stuNo);
                if (changeInfo != null)
                {
                    ChangeConfig changeConfig = new ChangeConfig();
                    this.tbStatusNow.Text = (changeConfig.GetChangeConfigByChangeCode(changeInfo.Change_Code)).Change_Descriptions.ToString();
                    this.SetdgvChanLog();
                }
                else
                {
                    this.tbStatusNow.Text = "在校";
                }
                //初始化部门班级信息
                Department department = new Department();
                IList<DepartmentInfo> depts = department.GetDepartments();
                this.cmbDepartment.DataSource = depts;
                this.cmbDepartment.DisplayMember = "Department_Name";
                this.cmbDepartment.ValueMember = "Department_Id";

                Classes myclass = new Classes();
                IList<ClassInfo> classInfo = myclass.GetClassesByDeptId(this.cmbDepartment.SelectedValue.ToString());
                this.cmbClass.DataSource = classInfo;
                this.cmbClass.DisplayMember = "ClassName";
                this.cmbClass.ValueMember = "ClassId";
                //显示所在班级
                for (int i = 0; i < cmbClass.Items.Count; i++)
                {
                    if (myTable.Rows[0]["所在班级"].ToString() == cmbClass.GetItemText(cmbClass.Items[i]))
                    {
                        this.cmbClass.SelectedIndex = i;
                        break;
                    }
                }
                //显示所在院系
                for (int i = 0; i < cmbDepartment.Items.Count; i++)
                {
                    if (myTable.Rows[0]["所在院系"].ToString() == cmbDepartment.GetItemText(cmbDepartment.Items[i]))
                    {
                        this.cmbDepartment.SelectedIndex = i;
                        break;
                    }
                }
            }
            else
            {
                MessageBox.Show("当前数据库中不存在学号为\""+this.tbStuNo.Text.Trim().ToString()+"\"的学生！");
            }
        }

        private void btuCancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void tbSubmit_Click_1(object sender, EventArgs e)
        {
            ChangeInfo changeInfo = new ChangeInfo();
            changeInfo.StuNo = this.tbStuNo.Text.Trim().ToUpper();
            changeInfo.Change_Code = this.cmbStatusChange.SelectedValue.ToString();
            changeInfo.Change_Descriptions = this.tbDescriptions.Text.Trim();
            changeInfo.Create_Date =DateTime.Now;
            changeInfo.Change_Id = System.Guid.NewGuid().ToString();
            Changes change = new Changes();  
            try
            {
                if (changeInfo.Change_Code =="C0001")
                {
                    string NowClass_Id = change.GetClass_Id(this.tbStuNo.Text.Trim().ToUpper());
                   // MessageBox.Show(NowClass_Id);
                    if( NowClass_Id!=this.cmbClass.SelectedValue.ToString() ){
                        Student student = new Student();
                        student.AlterStudentClassId(this.tbStuNo.Text.Trim(), this.cmbClass.SelectedValue.ToString());
                    }
                    else
                    {
                        MessageBox.Show("请选择转系后的学院班级！");
                        return;
                    }
                }
                change.InsertChange(changeInfo);
                this.tbDescriptions.Text = "";
                MessageBox.Show("学籍变更成功");
                this.SetdgvChanLog();
            }
            catch (Exception)
            {
                MessageBox.Show("学籍变更失败！");
                throw;
            }
          
        }

      

        
    }


}
